<?php



error_reporting(7);



$_LANG = array();

$_LANG['first'] = '首页';

$_LANG['previous'] = '上一页';

$_LANG['next'] = '下一页';

$_LANG['end'] = '尾页';

$_LANG['total'] = '共';

$_LANG['display'] = '显示';

$_LANG['to'] = '-';



/**

* 分页类

* 

* 

* 用于返回分页导航

* 使用例子 I:

* 

* <code>

* 

* // 默认每页显示 20 记录, 共 100 个记录

* $pager = new pager(100);

* 

* $results = $pager->result;

* 

* echo "<pre>";

* 

* print_r($results);

* 

* echo "</pre>";

* 

* </code>

* 

* 

* 

* 使用例子 II:

* 

* <code>

* 

* // 每页显示 10 记录, 共 100 个记录



* $pager = new pager(array('total' => 100, 'perpage' => 10));

* 

* $results = $pager->result;

* 

* echo "<pre>";

* 

* print_r($results);

* 

* echo "</pre>";

* 

* </code>

*/



class pager {

	/**

	* 偏移量, 通过当前页码及每页显示条数计算获得

	* 

	* @access private 

	* @var integer 

	*/



	var $offset = 0;



	/**

	* 记录总数

	* 

	* @access private 

	* @var integer 

	*/



	var $total = 0;



	/**

	* 当前页码

	* 

	* @access private 

	* @var integer 

	*/



	var $pagenum = 0;



	/**

	* 每页显示的记录数

	* 

	* @access private 

	* @var integer 

	*/



	var $perpage = 20;



	/**

	* 总页数, 通过总记录数及每页记录数计算获得

	* 

	* @access private 

	* @var integer 

	*/



	var $totalpages = 0;



	/**

	* 第 from 至 to 条记录中的 from

	* 

	* @access private 

	* @var integer 

	*/



	var $from = 0;



	/**

	* 第 from 至 to 条记录中的 to

	* 

	* @access private 

	* @var integer 

	*/



	var $to = 0;



	/**

	* 分页导航, 1 2 3 4 > ... >>

	* 

	* @access private 

	* @var string 

	*/



	var $pagenav = '';



	/**

	* 链接 index.php?action=test

	* 

	* @access private 

	* @var string 

	*/



	var $link = "";



	/**

	* 

	* @access private 

	* @var string 

	*/


	/**

         * 样式名css

         *

         * @var string

         */

        var $style = '';
        
        
        
       
	var $pagelink = '';



	/**

	* 

	* @access public 

	* @var array 

	*/



	var $result = array();



	function pager($options = array()) {

		/**

		* 

		* @global array $_LANG

		*/



		global $_LANG;



		if (!is_array($options)) {

			$this->total = $options;



			$this->pagenum = intval($_GET['pagenum']);

		} else {

			$this->total = intval($options['total']);



			$this->pagenum = isset($options['pagenum']) ? $options['pagenum'] : intval($_GET['pagenum']);



			if (isset($options['perpage'])) {

				$this->perpage = $options['perpage'];

			} 



			if (isset($options['link'])) {

				$this->link = $options['link'];

			} 
			
			if (isset($options['style']) && trim($options['style']) != "") {
                        	$this->style = $options['style'];
                        }

		} 



		$this->totalpages = ceil($this->total / $this->perpage);



		if ($this->pagenum < 1 OR empty($this->pagenum)) {

			$this->pagenum = 1;

		} elseif ($this->pagenum > $this->totalpages) {

			$this->pagenum = $this->totalpages;

		} 



		$this->from = ($this->pagenum-1) * $this->perpage + 1;



		if ($this->from < 0 OR $this->total == 0) {

			$this->from = 0;

		} 



		$this->to = $this->pagenum * $this->perpage;



		if ($this->to > $this->total) {

			$this->to = $this->total;

		} 



		$this->offset = intval(($this->pagenum-1) * $this->perpage);



		$this->pagenav = $this->pagenav();



		$this->result = array('total' => $this->total,



			'from' => $this->from,



			'to' => $this->to,



			'offset' => $this->offset,



			'pagenum' => $this->pagenum,



			'perpage' => $this->perpage,



			'pagenav' => $this->pagenav,

			'totalpages' => $this->totalpages

			);



		return $this->result;

	} 

	function pagenav() {

		global $_LANG;



		$space = "";



		$pagenum = intval($pagenum);



		$perpage = intval($this->perpage);



		if (!$this->link) {

			foreach($_GET AS $gk => $gv) {

				if ($gk != "pagenum") {

					// $gurl[] = $gk."=".$gv;

					$gurl[] = $gk . "=" . @urlencode($gv);

				} 

			} 



			if ($gurl AND is_array($gurl)) {

				$urls = trim(implode("&", $gurl));

			} 

			// $link = $_SERVER['PHP_SELF']."?".$urls;

			$this->link = $_SERVER['PHP_SELF'] . "?pagenum=####PAGENUM####&" . $urls;



			$this->pagelink = str_replace("####PAGENUM####", $this->pagenum, $this->link);

		} 



		$pagelink = '';



		if ($this->totalpages <= 1) {

			if (!empty($this->style)) { 
				return "<p  class=\"" . $this->style . "\"><strong>1</strong></p>";
			} else {
				return "<a class=\"curr\">1</a>";
			}

		} 



		if ($this->pagenum > 2) {

			$pagelink .= "<a class=num href=\"" . str_replace("####PAGENUM####", 1, $this->link) . "\">" . $_LANG['first'] . "</a>";

		} 



		if ($this->pagenum > 1) {

			if ($pagelink == "") {

				$pagelink .= "<a class=num href=\"" . str_replace("####PAGENUM####", ($this->pagenum - 1), $this->link) . "\">" . $_LANG['previous'] . "</a>";

			} else {

				$pagelink .= "$space<a class=num href=\"" . str_replace("####PAGENUM####", ($this->pagenum - 1), $this->link) . "\">" . $_LANG['previous'] . "</a>";

			} 

		} 



		$start = 1;



		$end = $this->totalpages;



		/*



                if (($this->pagenum - 6) > 0) {



                        $start = $this->pagenum - 6;



                }



                if (($this->pagenum + 6) < $this->totalpages) {



                        $end = $this->pagenum + 6;



                }



                */



		$start = 1;



		$end = $this->totalpages;



		$tmp_total = 8;



		$tmp = ceil($tmp_total / 2);



		if (($this->pagenum - $tmp) > 0) {

			$start = $this->pagenum - $tmp;

		} 



		if (($this->pagenum + $tmp) < $this->totalpages) {

			$end = $this->pagenum + $tmp;

		} 



		if ($this->pagenum < $tmp) {

			$end = $tmp_total;

		} 



		if ($this->pagenum > ($this->totalpages - $tmp)) {

			$start = $this->totalpages - $tmp_total;

		} 



		if ($start < 1) {

			$start = 1;

		} 



		if ($end > $this->totalpages) {

			$end = $this->totalpages;

		} 



		for ($i = $start; $i <= $end; $i++) {

			if ($this->pagenum == $i) {
				if (!empty($this->style)) {   
					$pagelink .= "$space<strong>$i</strong>";
				} else {
					$pagelink .= "$space<a class=\"curr\">$i</a>";
				}

			} else if ($i == 1) {

				$pagelink .= $space . "<a class=num href=\"" . str_replace("####PAGENUM####", $i, $this->link) . "\">$i</a>";

			} else {

				$pagelink .= $space . "<a class=num href=\"" . str_replace("####PAGENUM####", $i, $this->link) . "\">$i</a>";

			} 

		} 



		if ($this->pagenum < ($this->totalpages)) {

			$pagelink .= "$space<a class=num href=\"" . str_replace("####PAGENUM####", ($this->pagenum + 1), $this->link) . "\">" . $_LANG['next'] . "</a>";

		} 



		if ($this->pagenum < ($this->totalpages - 1)) {

			$pagelink .= "$space<a class=num href=\"" . str_replace("####PAGENUM####", $this->totalpages, $this->link) . "\">" . $_LANG['end'] . "</a>";

		} 


		if (!empty($this->style)) {                	
                	$pagelink = "<p class=\"" . $this->style . "\">" . $pagelink . "</p>";
                } else {
                	$pagelink = "<div id=\"pageLink\" class=\"pageLink\">" . $pagelink . "</div>";
                }

		return $pagelink;

	} 

} 



?>